package com.ming.springai.studydemo1;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Sinks;

@RestController
@RequestMapping("/test")  // 使用 @RequestMapping 来设置基础路径
public class TestController {


    @GetMapping("/hello")
    public String hello(@RequestParam String name) {
        return "hello world " + name;
    }

    @GetMapping(value = "/stream", produces = "text/event-stream;charset=UTF-8")
    public Flux<String> test(@RequestParam String name) {
        Sinks.Many<String> sink = Sinks.many().unicast().onBackpressureBuffer();
        sink.tryEmitNext("hello world " + name);
        new Thread(() -> {
            try {
                Thread.sleep(1000);
                sink.tryEmitNext("hello world " + "SleepName");
                sink.tryEmitComplete(); // 添加完成信号
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
        return sink.asFlux();
    }
}
